


/**
 * 获取数据
 */
function getData() {

    $.ajax({
        cache: true,
        type: 'get',
        url: "./js/formTitle.json",
        async: false,
        dataType: "json",
        error: function (err) {
            showMessage('请求失败', 'error');
        },
        success: (res) => {
            data = res.data
            console.log('请求成功', res)
        }
    });
}


/**
 * 初始化
 */
function init() {
    getData()
    createButton()
}


/**
 * 弹框提示
 * @param {string} msg  //消息内容
 * @param {string} type //消息类型
 */
function showMessage(msg, type) {
    let container = document.getElementById('container')
    let Message = document.createElement('div')
    Message.classList.add('message')
    if (msg.length < 10) Message.classList.add('small')
    else Message.classList.add('big')
    Message.setAttribute('id', 'msg')

    let msgIcon = document.createElement('div')
    msgIcon.classList.add('msg-icon`')

    let img = document.createElement('img')
    img.src = './images/' + type + '.png'
    msgIcon.appendChild(img)

    Message.appendChild(msgIcon)

    let content = document.createElement('div')
    let text = document.createTextNode(msg)
    content.appendChild(text)
    content.classList.add('msg-content')
    Message.appendChild(content)

    container.appendChild(Message)

    let count = (function () {
        let timer;
        let i = 0;
        function change(tar) {
            i++;
            let num = 1 - i / 100;
            if (num < 0.7) Message.style.top = num * 400 + 'px'
            Message.style.opacity = num;
            if (i === tar) {
                clearTimeout(timer);
                return false;
            }
            timer = setTimeout(function () {
                change(tar)
            }, 30)
        }
        return change;
    })()

    setTimeout(() => {
        count(100)

    }, 2000)
    setTimeout(() => {
        container.removeChild(Message)

    }, 5000)

}

function createButton() {
    if (data != null) {
        let container = document.getElementById('container')
        let btnList = document.createElement('div')
        btnList.classList.add('btnList')
        btnList.setAttribute("id", "btnList")
        data.forEach(element => {
            btnList.classList.add('btnList')
            let a = document.createElement('a')
            a.classList.add('item')
            a.setAttribute('id', 'item')
            a.href = element.url
            let title = document.createTextNode(element.title)
            a.appendChild(title)
            btnList.appendChild(a)
        });
        container.appendChild(btnList)
    } else {
        showMessage('发送了错误', 'error')
    }
}